import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/routes/tab_moments/controller/xindong_dongtai_actions_ctr.dart';
import 'package:xindong/routes/tab_moments/controller/xindong_dongtai_vm.dart';
import 'package:xindong/routes/tab_moments/details/xindong_dongtai_details_comment_cell.dart';
import 'package:xindong/routes/tab_moments/details/xindong_dongtai_details_controller.dart';
import 'package:xindong/routes/tab_moments/model/xindong_comment_class.dart';
import 'package:xindong/routes/tab_moments/model/xindong_dongtai_class.dart';
import 'package:xindong/routes/tab_moments/view/xindong_dongtai_list_view_cell.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/container/xindong_empty_container.dart';

class XinDongDongTaiDetailsYeMian extends StatelessWidget {
  final XinDongDongTaiClass model;
  final bool isSpace;
  final Function(XinDongDongTaiClass model)? deleteSuccessCallback;

  const XinDongDongTaiDetailsYeMian({super.key, required this.model, required this.isSpace, this.deleteSuccessCallback});

  @override
  Widget build(BuildContext context) {
    Get.create(() => XinDongDongTaiDetailsController(model), tag: model.dy_id);
    return Scaffold(
      appBar: XinDongAppBar("详情"),
      backgroundColor: Colors.white,
      resizeToAvoidBottomInset: false, //输入框抵住键盘 内容不随键盘滚动
      body: GetBuilder<XinDongDongTaiDetailsController>(
          tag: model.dy_id,
          builder: (controller) {
            return Column(
              children: [
                Expanded(child: _getBody(controller)),
                _bottomBarWidget(controller),
              ],
            );
          }),
    );
  }

  // 列表
  Widget _getBody(XinDongDongTaiDetailsController viewClass) {
    return CustomScrollView(
      controller: viewClass.scrollController,
      physics: const AlwaysScrollableScrollPhysics(),
      slivers: <Widget>[
        SliverToBoxAdapter(
          child: SizedBox(
            width: double.infinity,
            child: XinDongDongTaiListViewCell(
              model: viewClass.model,
              isDetails: true,
              isSpace: isSpace,
              onDelete: (XinDongDongTaiClass model) {
                try {
                  XinDongDongTaiVM.to.deleteWithClass(model);
                } catch (e) {}
              },
              commentCallback: (bool result) {},
            ),
          ),
        ),

        // 评论
        SliverToBoxAdapter(
          child: Container(
            color: DZColors.backgroundColor,
            padding: const EdgeInsets.only(left: 16, top: 10, bottom: 10),
            child: const Text(
              "评论",
              style: TextStyle(fontSize: 15, color: DZColors.titleColor, fontWeight: FontWeight.w600),
            ),
          ),
        ),

        // 评论列表
        XinDongEmptyContainer(
          dataList: viewClass.model.text,
          emptyTips: "快来抢沙发吧~",
          containSliver: true,
          child: SliverList(
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return XinDongDongTaiDetailsCommentCell(model: viewClass.model.text![index]);
              },
              childCount: viewClass.model.text?.length ?? 0,
            ),
          ),
        ),
      ],
    );
  }

  // 评论输入框
  Widget _bottomBarWidget(XinDongDongTaiDetailsController viewClass) {
    return GestureDetector(
      child: Container(
        height: kTabBarHeight + bottomBarHeight,
        padding: EdgeInsets.fromLTRB(16, 8, 16, 8 + bottomBarHeight),
        alignment: Alignment.topCenter,
        decoration: const BoxDecoration(
          color: DZColors.tabBar,
          border: Border(top: BorderSide(color: DZColors.dividerColor, width: 0.5)),
        ),
        child: Container(
          decoration: BoxDecoration(
            color: DZColors.sectionColor,
            borderRadius: BorderRadius.circular(48.0),
          ),
          child: TextField(
            controller: TextEditingController(text: viewClass.tempCommentText ??= ''),
            maxLines: 1,
            enabled: false,
            style: titleTextStyle,
            decoration: const InputDecoration(
              hintText: "说点什么吧～",
              hintStyle: placeholderTextStyle,
              contentPadding: EdgeInsets.only(left: 16, top: 8, bottom: 8, right: 16),
              border: OutlineInputBorder(
                gapPadding: 0,
                borderSide: BorderSide(
                  width: 0,
                  style: BorderStyle.none,
                ),
              ),
            ),
          ),
        ),
      ),
      onTap: () => XinDongDongTaiActionsCtr.to.requestComment(
        viewClass.model,
        sendResult: (bool result) {},
      ),
    );
  }
}
